package api

import (
	"FeedXmh/app/dao"
	"fmt"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

var Pig = pig{}

type pig struct {
}

func (a *pig) Get(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
	}

	params := r.GetQueryMap()
	id := gconv.String(params["id"])

	addr := gconv.String(params["addr"])
	sn := gconv.String(params["sn"])
	earmark := gconv.String(params["earmark"])
	pig_type := gconv.String(params["pig_type"])

	where := " 1=1 "
	if id != "" {
		where += " and id=" + id
	}
	if addr != "" {
		where += " and addr='" + addr + "'"
	}
	if sn != "" {
		where += " and sn='" + sn + "'"
	}
	if earmark != "" {
		where += " and earmark='" + earmark + "'"
	}
	if pig_type != "" {
		where += " and addr='" + pig_type + "'"
	}

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	if dao.CheckApiPermission(url, r.Method, userid) {
		if dao.IsAdmin(userid) || dao.IsService(userid) {

		} else {
			where += fmt.Sprintf(" and houseid in (SELECT houseid FROM t_user_house WHERE userid='%s')", userid)
		}
		db := g.DB(g.Cfg().GetString("custom.db_name"))
		model := db.Model("v_pig").Where(where)
		records, error := model.All()

		if error != nil {
			g.Log().Line().Error(error)
			res["msg"] = error.Error()
			res["code"] = -1
		} else {
			list := records.List()
			res["code"] = 0
			res["data"] = list
			res["count"] = len(list)
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *pig) Post(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
	}

	params := r.GetFormMap()
	//addr := gconv.String(params["addr"])
	//sn := gconv.String(params["sn"])
	earmark := gconv.String(params["earmark"])
	//pig_type := gconv.String(params["pig_type"])

	_, exist := params["id"]
	if exist {
		delete(params, "id")
	}

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	params["create_date"] = gtime.Now().String()
	params["create_user"] = userid
	if dao.CheckApiPermission(url, r.Method, userid) {
		gs, _ := dao.T_pig.GetList(fmt.Sprintf(" earmark='%s' ", earmark))
		if len(gs) > 0 {
			res["code"] = -1
			res["msg"] = "系统已经存在该耳标号"
		} else {
			_, err := dao.T_pig.Save(params)
			if err != nil {
				res["code"] = -1
				res["msg"] = err.Error()
			} else {
				res["code"] = 0
				res["msg"] = ""
			}
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *pig) Put(r *ghttp.Request) {

	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
	}

	params := r.GetFormMap()
	//addr := gconv.String(params["addr"])
	//sn := gconv.String(params["sn"])
	earmark := gconv.String(params["earmark"])
	//pig_type := gconv.String(params["pig_type"])

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	params["update_date"] = gtime.Now().String()
	params["update_user"] = userid
	id := gconv.String(params["id"])
	if dao.CheckApiPermission(url, r.Method, userid) {
		gs, _ := dao.T_device.GetList(fmt.Sprintf(" earmark='%s'  and id!=%s", earmark, id))

		if len(gs) > 0 {
			res["code"] = -1
			res["msg"] = "系统已经存在该耳标号"
		} else {
			_, err := dao.T_pig.Save(params)
			if err != nil {
				res["code"] = -1
				res["msg"] = err.Error()
			} else {
				res["code"] = 0
				res["msg"] = ""
			}
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *pig) Delete(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"data":  nil,
		"count": 0,
	}
	params := r.GetFormMap()

	id, exist := params["id"]
	if exist {
		claims := jwt.ExtractClaims(r)
		current_user := claims["userid"].(string)
		url := r.URL.Path
		if dao.CheckApiPermission(url, r.Method, current_user) {
			_, err := dao.T_pig.Delete(" id=" + gconv.String(id))
			if err != nil {
				res["code"] = -1
				res["msg"] = err.Error()
			} else {
				res["code"] = 0
				res["msg"] = ""
			}
		} else {
			res["code"] = -1
			res["msg"] = "您没有该操作权限"
		}
	} else {
		res["code"] = -1
		res["msg"] = "错误的参数"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *pig) SetPigNum(r *ghttp.Request) {

	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
	}

	params := r.GetFormMap()
	//addr := gconv.String(params["addr"])
	sn := gconv.String(params["sn"])
	pig_num := gconv.Int(params["pig_num"])
	son := gconv.Int(params["son"])

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	if dao.CheckApiPermission(url, r.Method, userid) {
		db := g.DB(g.Cfg().GetString("custom.db_name"))
		_, err := db.Exec(fmt.Sprintf("update t_hzfgw_node_para set pig_num='%d',son='%d' where sn='%s' ", pig_num, son, sn))
		if err != nil {
			res["code"] = -1
			res["msg"] = err.Error()
		} else {
			res["code"] = 0
			res["msg"] = ""
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}
